<?php
//unset user variables
    if(isset($fb_user_firstname))   unset($fb_user_firstname);
    if(isset($fb_user_lastname))   unset($fb_user_lastname);
    if(isset($fb_user_id))   unset($fb_user_id);
    if(isset($fb_network_id))   unset($fb_network_id);
    if(isset($fb_network_name))   unset($fb_network_name);
    if(isset($affiliations))   unset($affiliations);
    if(isset($_SESSION['fb_network_id']))   unset($_SESSION['fb_network_id']);
    if(isset($_SESSION['fb_network_name']))   unset($_SESSION['fb_network_name']);
    if(isset($_SESSION['fb_user_firstname']))   unset($_SESSION['fb_user_firstname']);
    if(isset($_SESSION['fb_user_lastname']))   unset($_SESSION['fb_user_lastname']);
    if(isset($_SESSION['fb_user_id']))   unset($_SESSION['fb_user_id']);
    if(isset($_SESSION['reach_verified']))   unset($_SESSION['reach_verified']);
    if(isset($_SESSION['reach_admin']))   unset($_SESSION['reach_admin']);
    if(isset($_SESSION['reach_tutor']))   unset($_SESSION['reach_tutor']);
    if(isset($_SESSION['banned']))   unset($_SESSION['banned']);
    if(isset($_SESSION['ulink']))   unset($_SESSION['ulink']);

//setup user variables
   $fql='SELECT first_name,last_name FROM user WHERE uid='.$fb_user.'';
   $data = $facebook->api_client->fql_query($fql);
   $_SESSION['fb_user_firstname'] = $data[0]['first_name'];
   $_SESSION['fb_user_lastname'] = $data[0]['last_name'];
   $_SESSION['fb_user_id'] = $fb_user;

//setup default user network variables, validate network
 $fql = 'SELECT affiliations FROM user WHERE uid='.$fb_user.'';
    $affiliations = $facebook->api_client->fql_query($fql);
    if(!isset($affiliations[0]['affiliations'][0]['nid']) ){
        echo "<h1><b> ERROR: Hmm we can't detect you have any Facebook network affiliations, so you might be a new user. Please try again later once you have added networks under account settings. Sorry :( </h1></b>";        exit;
    }

    if( ($affiliations[0]['affiliations'][0]['nid'] != "16777600") && ($affiliations[0]['affiliations'][1]['nid'] != "16777600") && ($affiliations[0]['affiliations'][2]['nid'] != "16777600") && ($affiliations[0]['affiliations'][3]['nid'] != "16777600") && ($affiliations[0]['affiliations'][4]['nid'] != "16777600")){       
         echo "<h1><b> ERROR: We can't detect that you belong to the University of Louisville's Facebook network.  Please check your network settings and try us after that.  Sorry :( </h1></b>";
         exit;
    }
    $_SESSION['fb_network_id'] = $affiliations[0]['affiliations'][0]['nid'];
    $_SESSION['fb_network_name'] = $affiliations[0]['affiliations'][0]['name'];

//see if this is a new user
    $sql = 'SELECT user_id from fb_users where user_id='.$_SESSION['user'].';';
    $result = $db->Execute($sql);
    $fb_user_id = $result->fields['user_id'];
//if not a user yet input user info
    $fb_network_id = $_SESSION['fb_network_id'];
    $fb_network_name = $_SESSION['fb_network_name'];
    $fb_first_name = $_SESSION['fb_user_firstname'];
    $fb_last_name = $_SESSION['fb_user_lastname'];
    if(!$fb_user_id)
     {
       $fb_user_id = $_SESSION['user'];
       $sql2="INSERT into fb_users values ( '$fb_user_id', '$fb_network_id', '$fb_first_name', '$fb_last_name','0','0','0','',NULL,'0',NULL)";
       $db->Execute($sql2);
     }

//setup up user Reach variables
     $sql = 'SELECT reach_verified,reach_tutor,banned,reach_admin,ulink_id from fb_users where user_id='.$_SESSION['user'].';';
     $result = $db->Execute($sql);
     $reach_veri=$_SESSION['reach_veri'] = $result->fields['reach_verified'];
     $reach_admin=$_SESSION['reach_admin'] = $result->fields['reach_admin'];
     $tutor=$_SESSION['tutor'] = $result->fields['reach_tutor'];
     $user_ban=$_SESSION['user_ban'] = $result->fields['banned'];
     $ulink=$_SESSION['ulink'] = $result->fields['ulink_id'];
       
    //check if user banned
    if($user_ban == 1)
    {
        echo "<h1><b> There has been problems reported to the Reach department about this user id, and unfortunately you have been
              banned from using Study Buddies.  Sorry :( <br><br> Please contact Reach if you would like access to Study Buddies again.</h1></b>";
        exit;
    }
?>
